package com.example.ignite.cotroller;

import com.example.ignite.service.Cache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/item")
@Slf4j
public class ItemRedisController {

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/redisput")
    public String redisput(Integer key, String value){
        Integer k = key==null? Math.abs(new Random().nextInt()) : key;
        String v = StringUtils.isNotBlank(value) ? value : k.toString();
        redisTemplate.opsForValue().set(k.toString(),v);
        return null;
    }

    @RequestMapping("/redisget")
    public Object redisget(Integer key){
        Integer k = key==null? Math.abs(new Random().nextInt()) : key;
        Object result = redisTemplate.opsForValue().get(k.toString());
//        log.info("redis result is {}",result);
        return result;
    }
}
